//
//  SHPTabBar.m
//  weibo
//
//  Created by songhuipan on 16/4/17.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

#import "SHPTabBar.h"
#import "FCCommon.h"
@interface SHPTabBar()
@property (nonatomic,strong)UIButton *composeButton;
@end
@implementation SHPTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.composeButton];
        
        // 设置tabbar的主题颜色，自定义一个tabBar，需要再次设置
        [self setTintColor:[UIColor orangeColor]];

    }
    return self;
}
/**
 *  中间加号按钮的懒加载
 *
 *  @return 中间加号按钮
 */
- (UIButton*)composeButton {
    if (!_composeButton) {
        
        _composeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_composeButton addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        // 根据背景图片的大小设置按钮的大小，只能决定宽高
        [_composeButton sizeToFit];
    }
    return _composeButton;
}
/**
 *  布局tabBar中控件位置的方法
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = kScreenWidth /5;
    CGFloat index = 0;
    
    for (UIView *view in self.subviews) {
        // tabBar的subViews有很多，我们只需要给UITabBarButton类型的按钮布局位置
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            view.w = width;
            view.x = index *width;
            index ++;
            
            if (index == 2) {
                index ++;
            }
        }
    }
    self.composeButton.center = CGPointMake(self.w *0.5, self.h *0.5);
}
/**
 *  监听中间加号按钮的点击事件
 *
 *  @param sender 中间加号按钮
 */
- (void)composeButtonClick:(UIButton*)sender {
    
    !self.composeButtonClick?:self.composeButtonClick();
}
@end



















